﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class Home : System.Web.UI.Page
{
    private int _userId;
    public int UserId
    {
        get { return _userId; }
        set { _userId = value; }
    }

    private int _userType;
    public int UserType
    {
        get { return _userType; }
        set { _userType = value; }
    }

    private int _pId;
    public int PId
    {
        get { return _pId; }
        set { _pId = value; }
    }

    public PersonalInformation pInfo;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Test code
        Session["UserType"] = 0;
        Session["UserId"] = 2;
        Session["pId"] = 1004;

        // Normal
        AccountChecked();
        if (Session["UserType"] == null || Session["UserId"] == null || Session["pId"] == null)
            Response.Redirect("~/Default.aspx");
        else
        {
            UserId = Int32.Parse(Session["UserId"].ToString());
            UserType = Int32.Parse(Session["UserType"].ToString());
            PId = Int32.Parse(Session["pId"].ToString());
        }
        Bind();
        Page.DataBind();
    }

    private void Bind()
    {
        pInfo = PersonalInformationManage.GetPersonalInformationById(PId);

        List<House> list = HouseManage.GetHousesChecked(UserId);

        this.rptHouseList.DataSource = list;
        this.rptHouseList.DataBind();

        List<House> notCheckedList = HouseManage.GetHousesNotChecked(UserId);
        this.rptUncheckedHouseList.DataSource = notCheckedList;
        this.rptUncheckedHouseList.DataBind();
        
    }

    protected void AccountChecked()
    {
        if (Session["UserType"] != null && Session["UserId"] != null)
        {
            if (Convert.ToInt32(Session["UserType"]) == 0)
            {
                int id = Convert.ToInt32(Session["UserId"]);
                if (!HouseHolderManage.IsChecked(id))
                {
                    // 跳转 提示页面
                    Response.Redirect("/Error.aspx");
                }
            }
        }
    }
}